import axios from 'axios';
import { getToken,setToken,setCookie } from './commonUtils'
import {CURRENT_URL} from './constant/'

var token = 'init';
var haveAlertTokenExpirt = false
var isAlerting = false
var isNetworkErr = false


var fetch = function(restAddress, method, inParams){

        if(process.env.NODE_ENV === 'development'){
            setToken('eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ7XCJzdGFmZkNvZGVcIjpcIlBQTTEwMDFcIixcIm9yZ0lkXCI6MTAwLFwib3JnTmFtZVwiOlwi5Lit5Zu955S15L-h6ZuG5Zui5YWs5Y-4XCIsXCJzdGFmZklkXCI6MTAwMSxcInN5c3RlbVVzZXJJZFwiOjEwMDEsXCJzdGFmZk5hbWVcIjpcIlBQTeezu-e7n-euoeeQhuWRmFwiLFwiYmluZE51bWJlclwiOlwiMTg2NDk3MTMzMzhcIixcInJlZ2lvbklkXCI6MTAwMDA4MTAwMDAwLFwicmVnaW9uTmFtZVwiOlwi5Lit5Zu9XCIsXCJjaGFubmVsSWRcIjpcIlwiLFwiY2hhbm5lbE5hbWVcIjpcIlwiLFwiaXNTdXBlck1hbmFnZXJcIjp0cnVlLFwibG9naW5lZERhdGVcIjpcIjIwMjAwMzE3MTQyODI5XCJ9IiwicHdkIjoiSSdtIHBhc3N3b3JkIiwiaWF0IjoxNTg0NDI2NTA5LCJleHAiOjE1ODQ1MTI5MDl9.echwJzsuxOzFiaUNNofuRzeotmBaw5_S9ijUAPV_aUw')
            setCookie('staffInfo','eyJzdGFmZkNvZGUiOiJQUE0xMDAxIiwib3JnSWQiOjEwMCwib3JnTmFtZSI6IuS4reWbveeUteS_oembhuWbouWFrOWPuCIsInN0YWZmSWQiOjEwMDEsInN5c3RlbVVzZXJJZCI6MTAwMSwic3RhZmZOYW1lIjoiUFBN57O757uf566h55CG5ZGYIiwiYmluZE51bWJlciI6IjE4NjQ5NzEzMzM4IiwicmVnaW9uSWQiOjEwMDAwODEwMDAwMCwicmVnaW9uTmFtZSI6IuS4reWbvSIsImNoYW5uZWxJZCI6IiIsImNoYW5uZWxOYW1lIjoiIiwiaXNTdXBlck1hbmFnZXIiOnRydWUsImxvZ2luZWREYXRlIjoiMjAyMDAzMTcxNDI4MjkifQ==')
        }
        // token="eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ7XCJzdGFmZkNvZGVcIjpcIlBQTTEwMDFcIixcIm9yZ0lkXCI6MTAwLFwib3JnTmFtZVwiOlwi5Lit5Zu955S15L-h6ZuG5Zui5YWs5Y-4XCIsXCJzdGFmZklkXCI6MTAwMSxcInN5c3RlbVVzZXJJZFwiOjEwMDEsXCJzdGFmZk5hbWVcIjpcIlBQTeezu-e7n-euoeeQhuWRmFwiLFwiYmluZE51bWJlclwiOlwiMTg2NDk3MTMzMzhcIixcInJlZ2lvbklkXCI6MTAwMDA4MTAwMDAwLFwicmVnaW9uTmFtZVwiOlwi5Lit5Zu9XCIsXCJjaGFubmVsSWRcIjpcIlwiLFwiY2hhbm5lbE5hbWVcIjpcIlwiLFwiaXNTdXBlck1hbmFnZXJcIjp0cnVlLFwibG9naW5lZERhdGVcIjpcIjIwMjAwNDEzMTAwODQyXCJ9IiwicHdkIjoiSSdtIHBhc3N3b3JkIiwiaWF0IjoxNTg2NzQzNzIyLCJleHAiOjE1ODY4MzAxMjN9.y-0XBifi3TC-ZORQLUvOBmCZCg76ctz-6YDHo9gv1CA";
        token = getToken();
        var url = CURRENT_URL + restAddress;

        var body = '';
        var fetchParam;
        if (method == 'get') {
            fetchParam = {
                url: url,
                method: method,
                params : inParams,
                headers: { 'Authorization': token },
                timeout: inParams.timeout ? inParams.timeout : 30000
            };
        } else {
            fetchParam = {
                url: url,
                method: method,
                data: inParams,
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': token
                },
                timeout: inParams.timeout ? inParams.timeout : 30000
            };
        }

        return new Promise((resolve, reject) => {

            axios(fetchParam)
                .then((response) => {
                	if(response.status != '200'){
                		//网络错误
                		reject(response.statusText);
                	}
                    let result = null;
                    if(typeof response.data == 'string'){
                        result = JSON.parse(response.data);
                    }else{

                        result = response.data;
                    }

                    if (result.meta.statusCd == '200') {
                        resolve(result.info);
                    } else {
                        if(result.meta.statusCd == '401'){
                            if(!isAlerting){
                                this.$alert('token失效，请重新登录！', '提示', {
                                          confirmButtonText: '确定',
                                          callback: action => {
                                            window.location.href = '/nppm-web'
                                            isAlerting = false;
                                }});
                            }
                        }else{
                            reject(result.meta.message);
                        }
                    }
                })
                .catch((error) => {
                    console.error('failed to get msg,url is:' + url);
                    console.error(error);
                    if ((error + '').indexOf('JSON Parse error') !== -1) {
                        reject('服务异常');
                    }else if ((error + '').indexOf('Network request failed') !== -1) {
                        error = '当前网络不可用,请检查网络';
                    }else if ((error + '').indexOf('Network Error') !== -1) {
                        error = '网络错误,无法连接到服务器';
                    }else if ((error + '').indexOf('timeout') !== -1) {
                        error = '请求超时,请检查网络';
                    }
                    reject(error);
                });
        });
}
export default fetch ;
